package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;

import com.itheima.pojo.Sorting;
import com.itheima.service.OrderService;
import com.itheima.service.SortingService;
import com.itheima.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴
 * @version V1.0
 * @Description
 * @Package com.itheima.controller
 * @date 2021/11/26 17:14
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @Reference
    private SortingService sortingService;

    @Reference
    private PayService payService;

    /**
     * 提交预约信息
     * @param map
     * @return
     */
    @PostMapping("/saveOrderInfo")
    public Result saveOrder(@RequestBody Map<String,String> map){

        // 验证码校验
        String code = jedisPool.getResource().get(map.get("telephone") + RedisConstant.SENDTYPE_ORDER);
        String validateCode = map.get("validateCode");
        if(!Objects.equals(code,validateCode)){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        // 提交预约
        try {
            return orderService.saveOrderInfo(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 根据Id查询预约信息
     * @param orderId
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer orderId){

        try {
            Order order = orderService.findMemberSetmealByOrderId(orderId);
            // 生成交易二维码连接
//            float aFloat = order.getSetmeal().getPrice();
//            int pr = (int)(aFloat*100f);
//            String price = String.valueOf(pr);
//            String setmealId = String.valueOf(order.getSetmeal().getId());

            Map<String, Object> map = payService.generateUrl("1","9");

            System.out.println(map);
            map.put("order",order);

            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     *查询所有分院
     * @return
     */
    @GetMapping("/findSortings")
    public Result findSortings(){
        try {
            List<Sorting> sortingList = orderService.findSortings();
            return new Result(true,"查询成功",sortingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    /**
     * 查询全部的分院信息
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Sorting> list = sortingService.findSortingAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


}
